//
//  PlanViewController.m
//  XhlTrip
//
//  Created by dllo on 15/8/24.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "PlanViewController.h"
#import "LeaonWorking.h"
#import "PlanViewCell.h"
#import "PlanModel.h"
#import "PlanDetailController.h"
#import "MBProgressHUD.h"

@interface PlanViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) NSMutableArray *planArr;
@property (nonatomic, retain) UITableView *tableView;


@end

@implementation PlanViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.planArr = [NSMutableArray array];
    }
    return self;
}

- (void)passId:(NSString *)str
{

    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:hud];
    hud.labelText = @"正在加载";
    [hud show:YES];
    // 形成点击进入
    NSString *ss = [NSString stringWithFormat:@"%@%@%@", @"https://chanyouji.com/api/destinations/plans/",str, @".json?page=1"];
    NSString *nn = [NSString stringWithFormat:@"%@%@", @"plan", str];
    [LeaonWorking netWorkingHanderGetGataWithUrl:ss pathname:nn resultBlock:^(id result) {
        [self.planArr addObjectsFromArray:result];
        [_tableView reloadData];

        [hud removeFromSuperview];
        [hud release];
    }];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
    self.view.backgroundColor = [UIColor whiteColor];
}
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    _tableView.rowHeight = 300;
    [_tableView release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.planArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlanViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"plan"];
    if (cell == nil) {
        cell = [[PlanViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"plan"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    PlanModel *model = [[[PlanModel alloc] init] autorelease];
    [model setValuesForKeysWithDictionary: self.planArr[indexPath.row]];
    cell.desLabel.text = [self.planArr[indexPath.row] objectForKey:@"description"];
    cell.planModel = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSString *str = [[self.planArr objectAtIndex:indexPath.row] objectForKey:@"description"];
   CGFloat height = [PlanViewCell heightWithText:str width:tableView.frame.size.width - 20 *Width];
    return height + 210 * Height;
    
}


/* 选中这一行进入下一页 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlanModel *model = [[PlanModel alloc] init];
    [model setValuesForKeysWithDictionary: self.planArr[indexPath.row]];
    NSString *desText = [self.planArr[indexPath.row] objectForKey:@"description"];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"行程";
    self.navigationItem.backBarButtonItem = backItem;
    PlanDetailController *planDetail = [[PlanDetailController alloc] init];
    NSString *str = [self.planArr[indexPath.row] objectForKey:@"id"];
    [planDetail passValue:str planModel:model nsstring:desText];
    [self.navigationController pushViewController:planDetail animated:YES];
    
}

- (void)dealloc
{
    [_tableView release];
    [_planArr release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
